package cn.qiyu5522.tk_ems.controller.admin;


import cn.qiyu5522.tk_ems.entity.Role;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.entity.dto.RoleDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-22
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @WebLog(description = "分页条件查询角色列表")
    @ApiOperation("角色列表查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true ,dataTypeClass = QueryParamDTO.class)
    @PostMapping("/query")
    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<RoleDTO> queryParamDTO) {
        return roleService.queryRoleListByCondition(queryParamDTO);
    }
    @WebLog(description = "角色新增--批量")
    @ApiOperation("角色新增")
    @ApiImplicitParam(name = "RoleDTOs", value = "角色新增", required = true,dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated(Role.Insert.class) @RequestBody List<RoleDTO> RoleDTOs) {
        return roleService.saveMore(RoleDTOs);
    }
    @ApiOperation("获取角色全部信息")
    @GetMapping("/queryRoleList")
    public R queryRoleList() {
        List<Role> list = roleService.list(null);
        return R.ok().data("roleList",list);
    }

    @WebLog(description = "角色编辑")
    @ApiOperation("角色编辑")
    @ApiImplicitParam(name = "roleDTO", value = "角色实体类对象", required = true, defaultValue = "{}", dataTypeClass = RoleDTO.class)
    @PutMapping("/edit")
    public R edit(@Validated(Role.Update.class) @RequestBody RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        boolean b = roleService.updateById(role);
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }
    @WebLog(description = "角色删除")
    @ApiOperation("角色删除")
    @ApiImplicitParam(name = "ids", value = "角色编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        boolean b = roleService.removeByIds(ids);
        return b ? R.ok().message("删除成功!"):R.error().message("删除失败");
    }
}
